#include "playgamesence.h"
#include <QPainter>
#include <QPaintEvent>
#include <QDebug>
#include <QThread>

PlayGameSence::PlayGameSence(QWidget *parent, int frame_size, int window_width, int window_height, int block_size, int player_num)
    : QWidget(parent)
    , f_size(frame_size)
    , w_width(window_width)
    , w_height(window_height)
    , block_size(block_size)
    , player_num(player_num)
{
    block_color.resize(window_height / block_size, std::vector<QColor>(window_width / block_size, Qt::red));

    players.push_back(Player(200, 200, 10, QColor(Qt::green)));

    timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, &PlayGameSence::updatePosition);

    timer->start(16);
}

void PlayGameSence::keyReleaseEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key_Escape)
    {
        emit exitGame();
    }
    Player& player = players[0];
    if(event->key() == Qt::Key_A)
    {
        player.left = false;
    }
    if(event->key() == Qt::Key_W)
    {
        player.up = false;
    }
    if(event->key() == Qt::Key_S)
    {
        player.down = false;
    }
    if(event->key() == Qt::Key_D)
    {
        player.right = false;
    }
}

void PlayGameSence::keyPressEvent(QKeyEvent *event)
{
    Player& player = players[0];
    if(event->key() == Qt::Key_A)
    {
        player.left = true;
    }
    if(event->key() == Qt::Key_W)
    {
        player.up = true;
    }
    if(event->key() == Qt::Key_S)
    {
        player.down = true;
    }
    if(event->key() == Qt::Key_D)
    {
        player.right = true;
    }
}

void PlayGameSence::setPlayerNum(int player_num)
{
    this->player_num = player_num;
}

void PlayGameSence::updatePosition()
{
    int dx = 0, dy = 0;
    Player& player = players[0];
    QPoint coor = player.getCoordirate();
    int radius = player.getRadius();
    if(player.left)
        dx -= 2;
    if(player.right)
        dx += 2;
    if(player.up)
        dy -= 2;
    if(player.down)
        dy += 2;

    if(dx != 0 && dy != 0)
    {
        dx /= 2;
        dy /= 2;
    }

    if(coor.x() - radius + dx < 0)
        dx = radius - coor.x();
    else if(coor.x() + radius + dx > w_width)
        dx = w_width - coor.x() - radius;

    if(coor.y() - radius + dy < 0)
        dy = radius - coor.y();
    else if(coor.y() + radius + dy > w_height)
        dy = w_height - coor.y() - radius;

    player.setDisplacement(dx, dy);
    qDebug() << dx << " " << dy;
    update();
}

void PlayGameSence::paintEvent(QPaintEvent *event)
{
    (void)(event);


    // 预渲染网格到QImage
    QImage gridImage(w_width, w_height, QImage::Format_RGB32);
//    gridImage.fill(Qt::white);

    QPainter preview_painter(&gridImage);
    QPen preview_pen;

    // 绘制网格线
    preview_pen.setColor(QColor("black"));
    preview_pen.setWidth(1);
    preview_painter.setPen(preview_pen);
    for(int i = 0; i <= w_width; i += block_size)
    {
        preview_painter.drawLine(i, 0, i, 640);
    }

    for(int i = 0; i <= w_height; i += block_size)
    {
        preview_painter.drawLine(0, i, 960, i);
    }

    // 绘制色块
    QBrush brush;
    brush.setStyle(Qt::SolidPattern);

    preview_pen.setColor(Qt::red);
    preview_painter.setPen(preview_pen);

    for(int i = 0; i < w_width; i += block_size)
    {
        for(int j = 0; j < w_height; j += block_size)
        {
            brush.setColor(block_color[j / block_size][i / block_size]);
            preview_painter.setBrush(brush);
            preview_painter.drawRect(i + 1, j + 1, block_size - 2, block_size - 2);
        }
    }

    // 绘制玩家
    preview_pen.setColor(QColor("black"));
    preview_pen.setWidth(1);
    preview_painter.setPen(preview_pen);
    for(auto player : players)
    {
        auto coor = player.getCoordirate();
        int radius = player.getRadius();
        brush.setColor(player.getColor());
        preview_painter.setBrush(brush);
        preview_painter.drawEllipse(QRect(coor.x() - radius, coor.y() - radius, radius * 2, radius * 2));
    }

    QPainter painter(this);


    painter.drawImage(f_size, f_size, gridImage);
}
